package com.xingqing.controller;

import com.xingqing.domain.entity.WorkExperience;
import com.xingqing.domain.vo.Result;
import com.xingqing.service.WorkExperienceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/work-experiences")
public class WorkExperienceController {

    @Autowired
    private WorkExperienceService workExperienceService;

    @GetMapping
    public Result<List<WorkExperience>> list() {
        log.info("查询所有工作经历");
        List<WorkExperience> workExperiences = workExperienceService.list();
        log.info("查询到{}条工作经历", workExperiences.size());
        return Result.success(workExperiences);
    }

    @GetMapping("/{id}")
    public Result<WorkExperience> getById(@PathVariable Long id) {
        log.info("根据ID查询工作经历，ID: {}", id);
        WorkExperience workExperience = workExperienceService.getById(id);
        log.info("查询到工作经历: {}", workExperience != null ? workExperience.getCompanyName() : "null");
        return Result.success(workExperience);
    }

    @GetMapping("/emp/{empId}")
    public Result<List<WorkExperience>> getByEmpId(@PathVariable Long empId) {
        log.info("根据员工ID查询工作经历，员工ID: {}", empId);
        List<WorkExperience> workExperiences = workExperienceService.getByEmpId(empId);
        log.info("查询到{}条工作经历", workExperiences.size());
        return Result.success(workExperiences);
    }

    @PostMapping
    public Result<Void> save(@RequestBody WorkExperience workExperience) {
        log.info("新增工作经历: {} - {}", workExperience.getCompanyName(), workExperience.getPosition());
        workExperienceService.save(workExperience);
        log.info("工作经历新增成功");
        return Result.success();
    }

    @PostMapping("/batch")
    public Result<Void> saveBatch(@RequestBody List<WorkExperience> workExperiences) {
        log.info("批量新增工作经历，数量: {}", workExperiences.size());
        workExperienceService.saveBatch(workExperiences);
        log.info("批量新增工作经历成功");
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @RequestBody WorkExperience workExperience) {
        log.info("更新工作经历，ID: {}, 公司: {}", id, workExperience.getCompanyName());
        workExperience.setId(id);
        workExperienceService.updateById(workExperience);
        log.info("工作经历更新成功");
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        log.info("删除工作经历，ID: {}", id);
        workExperienceService.deleteById(id);
        log.info("工作经历删除成功");
        return Result.success();
    }
}